package com.handtransaction.listener;

import com.handtransaction.entity.Category;
import com.handtransaction.entity.Goods;
import com.handtransaction.service.CategoryService;
import com.handtransaction.service.impl.GoodsServiceImpl;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.*;

/**
 * @author YAO
 * @create 2021-03-23 22:33
 */
@WebListener
public class GoodsListener implements ServletContextListener {
    private ServletContext sc = null;


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        GoodsServiceImpl goodsService = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(GoodsServiceImpl.class);
        List<Goods> list = goodsService.QueryGoods();
        sc = sce.getServletContext();
        sc.setAttribute("Goodslist" ,list);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        sc = sce.getServletContext();
        sc.removeAttribute("Goodslist");
    }
}
